package elitech.vietnam.myfashion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
	private ImageView bmImage;
	private File cacheDir;
	private boolean isThumb = false;
	private Context mContext;
	private int mSampleSize = 1;
	
	public DownloadImageTask(Context context, ImageView bmImage, boolean isThumb, int SampleSize) {
		this.bmImage = bmImage;
		this.mContext = context;
		this.isThumb = isThumb;
		this.mSampleSize = SampleSize;
		cacheDir = mContext.getCacheDir();
	}

	@Override
	protected Bitmap doInBackground(String... urls) {
		String urldisplay = "";
		if (isThumb) {
			urldisplay = Const.SERVER_IMAGE_THUMB_URL + urls[0];
		} else {
			urldisplay = Const.SERVER_IMAGE_URL + urls[0];
			if (urls[0].indexOf("post") != -1)
				urldisplay = urldisplay.replace("app/", "");
			if (urls[0].indexOf("account") != -1) {
				urldisplay = urldisplay.replace("app/", ""); Log.e("account", urldisplay);}
		}
		Bitmap bm = readPhotoCache(urldisplay.replace("/", "").replace(".", "")
				.replace(":", ""));
		if (bm == null) {
			try {
				Log.w("Shopping", urldisplay);
				InputStream in = new java.net.URL(urldisplay).openStream();
				Options options = new Options();
				options.inSampleSize = mSampleSize;
				bm = BitmapFactory.decodeStream(in);
				writePhotoCache(bm, urldisplay.replace("/", "")
						.replace(".", "").replace(":", ""));
			} catch (Exception e) {
			}
			return readPhotoCache(urldisplay.replace("/", "")
						.replace(".", "").replace(":", ""));
		} else
			return bm;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		if (result != null){
			Animation anim_in  = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in); 

		    AnimationSet animation = new AnimationSet(false); // change to false
		    animation.addAnimation(anim_in);
		    animation.setDuration(400);
		    animation.setRepeatCount(1);
		    
			bmImage.setImageBitmap(result);
			bmImage.setAnimation(animation);
		}
	}

	public Bitmap readPhotoCache(String Id) {
		File f = new File(cacheDir, Id);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(f);
			Options options = new Options();
			options.inSampleSize = mSampleSize;
			return BitmapFactory.decodeStream(fis, new Rect(), options);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	public void writePhotoCache(Bitmap photo, String Id) {
		File f = new File(cacheDir, Id);

		try {
			FileOutputStream out = new FileOutputStream(f);
			photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
			out.flush();
			out.close();

		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}
}
